﻿using System;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Sustainalytics.Events.DataAccessLayer
{
    public class EventsDatabase : IEventsDatabase
    {
        private readonly SqlDatabase _database;
        private readonly int _commandTimeout;

        public EventsDatabase()
        {
            _commandTimeout = 60 * 60;
        }

        public EventsDatabase(string connectionString, int commandTimeout)
        {
            if (string.IsNullOrEmpty(connectionString))
                throw new Exception("Connection string must be provided!");

            _database = new SqlDatabase(connectionString);
            _commandTimeout = commandTimeout;
        }

        public IDataReader CompanyProfiles()
        {
            var command = new SqlCommand
            {
                CommandText = "SELECT * FROM dbo.CompanyProfiles",
                CommandType = CommandType.Text,
                CommandTimeout = _commandTimeout
            };

            return _database.ExecuteReader(command);
        }

        public IDataReader EventIndicatorTemplates()
        {
            var command = new SqlCommand
            {
                CommandText = "SELECT * FROM dbo.EventIndicatorTemplates",
                CommandType = CommandType.Text,
                CommandTimeout = _commandTimeout
            };

            return _database.ExecuteReader(command);
        }

        public IDataReader ControversyIndicators(List<Guid> companyIds)
        {
            var command = new SqlCommand
            {
                CommandText = "spGetControversyIndicatorsByCompanyIds",
                CommandType = CommandType.StoredProcedure,
                CommandTimeout = _commandTimeout
            };

            var ids = new XElement("CompanyIds", companyIds.Select(id => new XElement("Id", id)));

            command.Parameters.AddWithValue("@CompanyIds", ids.ToString());

            return _database.ExecuteReader(command);
        }

        public IDataReader EventIndicators(List<Guid> companyIds)
        {
            var command = new SqlCommand
            {
                CommandText = "spGetEventIndicatorsByCompanyIds",
                CommandType = CommandType.StoredProcedure,
                CommandTimeout = _commandTimeout
            };

            var ids = new XElement("CompanyIds", companyIds.Select(id => new XElement("Id", id)));

            command.Parameters.AddWithValue("@CompanyIds", ids.ToString());

            return _database.ExecuteReader(command);
        }

        public IDataReader EventIndicatorComments(List<Guid> companyIds)
        {
            var command = new SqlCommand
            {
                CommandText = "spGetEventIndicatorCommentsByCompanyIds",
                CommandType = CommandType.StoredProcedure,
                CommandTimeout = _commandTimeout
            };

            var ids = new XElement("CompanyIds", companyIds.Select(id => new XElement("Id", id)));

            command.Parameters.AddWithValue("@CompanyIds", ids.ToString());

            return _database.ExecuteReader(command);
        }

        public IDataReader Incidents(List<Guid> companyIds)
        {
            var command = new SqlCommand
            {
                CommandText = "spGetIncidentsByCompanyIds",
                CommandType = CommandType.StoredProcedure,
                CommandTimeout = _commandTimeout
            };

            var ids = new XElement("CompanyIds", companyIds.Select(id => new XElement("Id", id)));

            command.Parameters.AddWithValue("@CompanyIds", ids.ToString());

            return _database.ExecuteReader(command);
        }

        public IDataReader NewsArticles()
        {
            var command = new SqlCommand
            {
                CommandText = "SELECT * FROM dbo.NewsArticles",
                CommandType = CommandType.Text,
                CommandTimeout = _commandTimeout
            };

            return _database.ExecuteReader(command);
        }

        public IDataReader Alerts(DateTime createdAfter)
        {
            var command = new SqlCommand
            {
                CommandText = "SELECT * FROM dbo.Alerts where Created > '" + createdAfter.Date + "'",
                CommandType = CommandType.Text,
                CommandTimeout = _commandTimeout
            };

            return _database.ExecuteReader(command);
        }
    }
}
